#include "errorcodeswidget.h"
#include "modulewidget.h"
#include <QDebug>

ErrorCodesWidget::ErrorCodesWidget(QWidget *parent) : QWidget(parent)
{
  pModuleWidget = 0;
  moduleList.clear();
}

void ErrorCodesWidget::setModuleList(std::list<Module> m)
{
  if(pModuleWidget == 0) {
    pModuleWidget = new ModuleWidget(this);
    pModuleWidget->buildWidget(m);
  } else {
    pModuleWidget->updateModuleListView(m);
  }
}

void ErrorCodesWidget::buildWidget()
{
  QSizePolicy sizePolicyModule(QSizePolicy::Expanding, QSizePolicy::Preferred);
  QSizePolicy sizePolicyTable(QSizePolicy::Expanding, QSizePolicy::Expanding);

  pVerticalLayout = new QVBoxLayout(this);

  if(pModuleWidget == 0) {
    pModuleWidget = new ModuleWidget(this);
  }

  pVerticalLayout->setContentsMargins(1, 1, 1, 1);

  sizePolicyModule.setHorizontalStretch(0);
  sizePolicyModule.setVerticalStretch(0);
  sizePolicyModule.setHeightForWidth(pModuleWidget->sizePolicy().hasHeightForWidth());

  pModuleWidget->setSizePolicy(sizePolicyModule);
  pModuleWidget->setMinimumSize(QSize(0, 20));
  pModuleWidget->setMaximumSize(QSize(16777215, 80));
  pModuleWidget->setBaseSize(QSize(0, 20));

  pVerticalLayout->addWidget(pModuleWidget);

  pVerticalSpacer = new QSpacerItem(20, 3, QSizePolicy::Minimum, QSizePolicy::Minimum);
  pVerticalLayout->addItem(pVerticalSpacer);

  pTablewWidget = new ErrorCodesTableView("error_code", this);
  sizePolicyTable.setHorizontalStretch(0);
  sizePolicyTable.setVerticalStretch(0);
  sizePolicyTable.setHeightForWidth(pTablewWidget->sizePolicy().hasHeightForWidth());
  pTablewWidget->setSizePolicy(sizePolicyTable);

//  connect(pTablewWidget, SIGNAL(setErrorCode(const Errorcode &)), this, SLOT(setErrorCode(const Errorcode&)));
  connect(pModuleWidget, SIGNAL(moduleWidgetUpdated(const Module &)), pTablewWidget, SLOT(updateTable(const Module &)));

  pVerticalLayout->addWidget(pTablewWidget);
}

ErrorCodesTableView *ErrorCodesWidget::getErrorCodesTableView() const
{
  return pTablewWidget;
}
